<script setup lang="ts">
import { computed, inject, reactive, toRef, watchEffect } from 'vue';
const data: any = inject('data');
const pc_pnd_pe_obj_show = reactive<any[]>([]) 
const pc_pub_info_obj_show = reactive<any[]>([])


//公共信息
const map = {
  PAP: {
    type: '行政处罚信息',
    amountKey: 'PF04AJ01'
  },
  PCE: {
    type: '强制执行信息',
    amountKey: 'PF03AJ01'
  },
  POT: {
    type: '欠税信息',
    amountKey: 'PF01AJ01'
  },
  PCJ: {
    type: '民事判决信息',
    amountKey: 'PF02AJ01'
  }
}

watchEffect(() => {
  if(!data.value.pc_pnd_pe_obj) return;
  data.value.pc_pnd_pe_obj.PND.forEach((item:any) => {
  if(item.PE01AD03 == '欠费' ){
  pc_pnd_pe_obj_format(item)
  }

})
for (const key in data.value?.pc_pub_info_obj) {
      pc_pub_info_obj_format(key, data.value?.pc_pub_info_obj[key])
}
})
const pc_pqo_pc_list = computed(() => {
  return data.value.pc_pqo_pc_list||[]
})
function pc_pnd_pe_obj_format(item: any){
  const type = item.PE01AD02
  const index = getIndex(type)
  if(index == -1){
    pc_pnd_pe_obj_show.push({
      type,
      count:1,
      amount: item.PE01AD03 == '欠费' ? Number(item.PE01AJ01).toFixed(2) : 0
    })
  }else{
    pc_pnd_pe_obj_show[index].count++
    pc_pnd_pe_obj_show[index].amount += item.PE01AD03 == '欠费' ? item.PE01AJ01 : 0
    pc_pnd_pe_obj_show[index].amount = Number(pc_pnd_pe_obj_show[index].amount).toFixed(2)
    
  }
}
function getIndex(type: string){
  return pc_pnd_pe_obj_show.findIndex(item => item.type === type)
}


function pc_pub_info_obj_format(key:any, arr: any[]){
  if(!map[key]) return;
  const type = map[key].type;
  arr.forEach((item: any) => {
    const index = pc_pub_info_obj_show.findIndex(item => item.type === type)
    if(index == -1){
      pc_pub_info_obj_show.push({
        type,
        count: 1,
        amount: item[map[key].amountKey]
      })
    }else{
      const p = pc_pub_info_obj_show[index]
      p.count++
      p.amount = (Number(item[map[key].amountKey]) + Number(p.amount)).toFixed()
    }
  })
}

</script>
<template>
  <div class="h3 title">（4）非信贷交易信息概要</div>
  <table>
    <caption>
        后付费业务欠费信息汇总
      </caption>
    <tbody>
     
        <tr>
        <th>业务类型</th>
        <th>账户数</th>
        <th>欠款金额</th>
      </tr>
      <tr v-for="(item,i) in pc_pnd_pe_obj_show" :key="i">
        <td>{{ item.type }}</td>
        <td>{{ item.count }}</td>
        <td>{{ item.amount }}</td>
      </tr>
    </tbody>
  </table>

  <div class="h3 title">（5）公共信息概要</div>
  <table>
    <caption>公共信息汇总</caption>

    <tbody>
      <tr>
        <th>信息类型</th>
        <th>记录数</th>
        <th>涉及余额</th>
      </tr>
      <tr v-for="(item ,i) in pc_pub_info_obj_show" :key="i">
        <td>{{ item.type }}</td>
        <td>{{ item.count }}</td>
        <td>{{ item.amount }}</td>
      </tr>
    </tbody>
  </table>

  <div class="h3 title">（6）查询记录概要</div>
  <table>
    <tbody>
      <tr>
        <th colspan="2">最近 1 个月内的查询机构数</th>
        <th colspan="3">最近 1 个月内的查询次数</th>
        <th colspan="3">最近 2 年内的查询次数</th>
      </tr>
      <tr>
        <th>贷款审批</th>
        <th>信用卡审批</th>
        <th>贷款审批</th>
        <th>信用卡审批</th>
        <th>本人查询</th>
        <th>贷后管理</th>
        <th>担保资格审查</th>
        <th>特约商户实名审查</th>
      </tr>
      <tr>
        <td>{{ pc_pqo_pc_list[0]?.PC05BS01 }}</td>
        <td>{{ pc_pqo_pc_list[0]?.PC05BS02 }}</td>
        <td>{{ pc_pqo_pc_list[0]?.PC05BS03 }}</td>
        <td>{{ pc_pqo_pc_list[0]?.PC05BS04 }}</td>
        <td>{{ pc_pqo_pc_list[0]?.PC05BS05 }}</td>
        <td>{{ pc_pqo_pc_list[0]?.PC05BS06 }}</td>
        <td>{{ pc_pqo_pc_list[0]?.PC05BS07 }}</td>
        <td>{{ pc_pqo_pc_list[0]?.PC05BS08 }}</td>
      </tr>
    </tbody>
  </table>


</template>